
package com.x52im.rbchat.utils;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;

import com.eva.epc.common.util.CommonUtils;
import com.x52im.rbchat.R;
import com.x52im.rainbowchat.http.logic.dto.RosterElementEntity;
import com.x52im.rbchat.logic.chat_root.model.MessageExt;

import java.text.MessageFormat;

/**
 * 显示各种Android系统通知的辅助类。
 *
 * @since 1.0
 */
public class NotificationPromptHelper {
    // 【重要说明】每添加一种Notification则务必去方法cancalAllNotification()中加入清除处理！！！！
//	/** 点击进入APP的全局Notification */
//	public final static int NOTIFICATION_UNIQE_IDENT_ID_APP = 0;

    /** 收到了加好友请求时的提示 */
    public final static int NOTIFICATION_UNIQE_IDENT_ID_ADD$FRIEND$REQUEST = 1;
    /** 服务端反馈给请求发起者，加好友请求在服务端处理中出现的各种错误时的提示 */
    public final static int NOTIFICATION_UNIQE_IDENT_ID_ADD$FRIEND$REQUEST_RESPONSE$FOR$ERROR_SERVER$TO$A = 2;
    /** 好友请求被对方成功处理后时的提示（被加者同意后服务端会同时向请求者和被加者送出成功指令） */
    public final static int NOTIFICATION_UNIQE_IDENT_ID_NEW$FRIEND$ADD$SUCESS = 3;
    /** 加好友被拒绝时的提示 */
    public final static int NOTIFICATION_UNIQE_IDENT_ID_ADD$FRIEND$BE$REJECT = 4;
    /** 相关处理界面处于后台时接收到音视频聊天请求时的提示 */
    public final static int NOTIFICATION_UNIQE_IDENT_ID_VOICE$VIDEO$CHAT$REQUEST = 5;
    /** 相关处理界面处于后台时接收到好友发过来的角色指令时的提示 */
    public final static int NOTIFICATION_UNIQE_IDENT_ID_RECIEVED$FRIEND$SCENSE$CMD = 6;
    /** 相关处理界面处于后台时接收到好友发过来的聊天消息时的提示 */
    public final static int NOTIFICATION_UNIQE_IDENT_ID_RECIEVED$FRIEND$MESSAGE = 7;
//	/** 相关处理界面处于后台时接收到好友发过来的蓝牙交互控制请求消息时的提示 */
//	public final static int NOTIFICATION_UNIQE_IDENT_ID_RECIEVED$FRIEND$BLUETOOTH$INTERACT$CONTROL$PERMISSION = 8;
    /** 相关处理界面处于后台时接收到实时语音聊天请求时的提示 */
    public final static int NOTIFICATION_UNIQE_IDENT_ID_REAL$TIME$VOICE$CHAT$REQUEST = 9;

    /** 相关处理界面处于后台时接收到好友发过来的临时聊天消息时的提示 */
    public final static int NOTIFICATION_UNIQE_IDENT_ID_RECIEVED$TEMP$MESSAGE = 10;
    /** 相关处理界面处于后台时接收到好友发过来的BBS聊天消息时的提示 */
    public final static int NOTIFICATION_UNIQE_IDENT_ID_RECIEVED$BBS$MESSAGE = 11;

    /** "我"被邀请成功加入群的提示 */
    public final static int NOTIFICATION_UNIQE_IDENT_ID_MYSELF$BE$INVITED = 12;

//	/**
//	 * 获得回到APP的Notification.
//	 * 
//	 * @param context
//	 */
//	public static void showBackToAPPNotification(Context context)
//	{
//		Notification notification = GeniusService.createAppNotification(
//				context, MyApplication.getInstance(context).getLocalUserInfoProvider());
//		if(notification == null)
//			return;
//		
//		// 以下flag保证点击系统的“清空”按钮不会清除此Notification
////		notification.flags |= Notification.FLAG_ONGOING_EVENT;
//		notification.flags |= Notification.FLAG_NO_CLEAR;
//		
//		NotificationManager manager = (NotificationManager) context
//				.getSystemService(Context.NOTIFICATION_SERVICE);
//        manager.notify(NotificationPromptHelper.NOTIFICATION_UNIQE_IDENT_ID_APP//1
//				, notification);
//	}

    /**
     * 收到了加好友请求时的提示（由服务端通知被请求者）.
     *
     * @param context
     * @param srcUserInfo
     */
    public static void showAddFriendRequestNotivication(Context context, RosterElementEntity srcUserInfo) {
        NotificationHelperEx.addNotificaction(
                NOTIFICATION_UNIQE_IDENT_ID_ADD$FRIEND$REQUEST, context
//				, new Intent()//IntentFactory.createPortalIntent(context)
                , IntentFactory.createFriendReqProcessIntent(context, srcUserInfo)
                , R.drawable.main_alarms_sns_addfriendrequest_message_icon
                //, "Received a request to be lover from "+srcUserInfo.getNickname()+"..."
                , MessageFormat.format(context.getResources().getString(R.string.notification_sns_add_friend_reauest_ticker_text), srcUserInfo.getNickname())
                , context.getResources().getString(R.string.notification_sns_add_friend_reauest_info_title)
                , MessageFormat.format(context.getResources().getString(R.string.notification_sns_add_friend_reauest_info_text), srcUserInfo.getNickname())
                , true, true, false);
    }

    /**
     * 服务端反馈给请求发起者，加好友请求在服务端处理中出现的各种错误时的提示（由服务端通知请求发起者）.
     *
     * @param context
     * @param errorMsg
     */
    public static void showAddFriendRequest_RESPONSE$FOR$ERROR_SERVER$TO$ANotivication(Context context, String errorMsg) {
        NotificationHelperEx.addNotificaction(
                NOTIFICATION_UNIQE_IDENT_ID_ADD$FRIEND$REQUEST_RESPONSE$FOR$ERROR_SERVER$TO$A, context
                , new Intent()//IntentFactory.createFriendInfoIntent(context, friendInfo, false)
                , R.drawable.main_alarms_system_message_icon
                , context.getResources().getString(R.string.notification_sns_add_friend_response_from_server_error_ticker_text)
                , context.getResources().getString(R.string.notification_sns_add_friend_response_from_server_error_info_title)
                , errorMsg, true, true, false);
    }

    /**
     * 新添加的好友成列加入到好友列表了（由服务端通知请求发起者和
     * 被请求者：被加者同意后服务端会同时向请求者和被加者送出成功指令）.
     *
     * @param context
     */
    public static void showNewFriendAddSucessNotivication(Context context, String newFriendNickName, String friendUID) {
        NotificationHelperEx.addNotificaction(
                NOTIFICATION_UNIQE_IDENT_ID_NEW$FRIEND$ADD$SUCESS, context
                , IntentFactory.createChatIntent(context, friendUID)
                , R.drawable.main_alarms_sns_addfriendok_message_icon
//				, "Successfully completed to add a new lover "+newFriendNickName
                , MessageFormat.format(context.getResources().getString(R.string.notification_sns_add_friend_success_ticker_text), newFriendNickName)
                , context.getResources().getString(R.string.notification_sns_add_friend_success_info_title)
                , MessageFormat.format(context.getResources().getString(R.string.notification_sns_add_friend_success_info_text), newFriendNickName)
                , true, true, false);
    }

    /**
     * "我"被邀请进入了群聊的系统通知.
     *
     * @param context
     */
    public static void showMyselfBeInvitedGroupNotivication(Context context, String groupName, String groupId) {
        NotificationHelperEx.addNotificaction(
                NOTIFICATION_UNIQE_IDENT_ID_MYSELF$BE$INVITED, context
                , IntentFactory.createGroupChatIntent(context, groupId, groupName)
                , R.drawable.main_alarms_sns_addgroupok_cmd_icon
                , "您已加入群聊\"" + groupName + "\""
                , "已加入群聊"
                , "您已加入群聊\"" + groupName + "\"，点击进入..."
                , true, true, false);
    }

    /**
     * 加好友被拒绝时的提示（由服务端提示加好友发起人A）.
     *
     * @param context
     */
    public static void showAddFriendBeRejectNotivication(Context context, RosterElementEntity userInfoFromServer) {
        NotificationHelperEx.addNotificaction(
                NOTIFICATION_UNIQE_IDENT_ID_ADD$FRIEND$BE$REJECT, context
                , IntentFactory.createFriendInfoIntent(context, userInfoFromServer)//, false)
                , R.drawable.main_alarms_sns_addfriendreject2r_message_icon
//				, userInfoFromServer.getNickname()+" refused your lover request"
                , MessageFormat.format(context.getResources().getString(R.string.notification_sns_add_friend_reject_ticker_text), userInfoFromServer.getNickname())
                , context.getResources().getString(R.string.notification_sns_add_friend_reject_info_title)
                , MessageFormat.format(context.getResources().getString(R.string.notification_sns_add_friend_reject_info_text), userInfoFromServer.getNickname())
                , true, true, false);
    }

    //############################################################################ 2013-11-16因当时不稳定先去掉了新音视频框架！ S
//	/**
//	 * 相关处理界面处于后台时接收到音视频聊天请求时的提示（来自发起人A）.
//	 * 
//	 * @param context
//	 * @param friendNickName 昵称
//	 */
//	public static void showVoiceAndVideoRequestNotivication(Context context
//			, String friendUID, String friendNickName)
//	{
//		NotificationHelper.addNotificaction(
//				NOTIFICATION_UNIQE_IDENT_ID_VOICE$VIDEO$CHAT$REQUEST, context
//				, IntentFactory.createVideoCallComeIntent(context, friendUID)
//				, R.drawable.sns_friend_list_form_item_defult_portrait
//				, friendNickName+"请求你进行音视频聊天"
//				, "请求音视频聊天"
//				, "注意，"+friendNickName+"正在请求你进行音视频聊天，请打开界面处理之.", true, true);
//	}
    //############################################################################ 2013-11-16因当时不稳定先去掉了新音视频框架！ E

    /**
     * 相关处理界面处于后台时接收到音视频聊天请求时的提示（来自发起人A）. -- AnyChat
     *
     * @param context
     * @param friendNickName 昵称
     */
    public static void showVoiceAndVideoRequestNotivication(Context context, String friendUID, String friendNickName) {
        NotificationHelperEx.addNotificaction(
                NOTIFICATION_UNIQE_IDENT_ID_VOICE$VIDEO$CHAT$REQUEST, context
                , IntentFactory.createChatIntent(context, friendUID)//new Intent() //IntentFactory.createVideoCallComeIntent_anychat(context, friendUID)
                , R.drawable.main_alarms_chat_opr_realtime_video_icon
//				, friendNickName+" requests you to audio and video chat"
                , MessageFormat.format(context.getResources().getString(R.string.notification_video_and_voice_chat_request_ticker_text), friendNickName)
                , context.getResources().getString(R.string.notification_video_and_voice_chat_request_info_title)
                , MessageFormat.format(context.getResources().getString(R.string.notification_video_and_voice_chat_request_info_text), friendNickName)
                , true, true, true);
    }

    /**
     * 相关处理界面处于后台时接收到好友发过来的聊天消息时的提示（来自发起人A）.
     *
     * @param context
     * @param friendNickName 昵称
     */
    public static void showRecievedFriendMessageNotivication(Context context
            , String friendUID, String friendNickName, String message) {
        NotificationHelperEx.addNotificaction(
                NOTIFICATION_UNIQE_IDENT_ID_RECIEVED$FRIEND$MESSAGE, context
                , IntentFactory.createChatIntent(context, friendUID)//new Intent()
                , R.drawable.main_alarms_chat_message_icon
                , MessageFormat.format(context.getResources().getString(R.string.notification_text_chatting_message_ticker_text), friendNickName, message)
                , MessageFormat.format(context.getResources().getString(R.string.notification_text_chatting_message_info_title), friendNickName)
                , message, true, false, false);
    }

    /**
     * 收到一个临时聊天消息哦.
     * <p>目前此Notivication里只处理了“普通文本消息”哦！
     *
     * @param context //@param msgContent 存放的内容而是TextMessage对象的JSON文本，如果读取聊天文本消息则需要进行JSON解析哦
     * @param msg     消息内容，纯文本字串，可能是聊天文字、图片文件名或语音文件名等，但一定不是JSON字串
     */
    public static void showATempChatMsgNotivication(Context context
            , int msgType, String msg, String fromUid, String fromNickName) {
        // 自kchat2.2(20140212)后，此字段将用于消息内容的显示
        String messageContentForShow = MessageExt.parseMessageContentPreview(context, msg, msgType);

        NotificationHelperEx.addNotificaction(
                NOTIFICATION_UNIQE_IDENT_ID_RECIEVED$TEMP$MESSAGE, context
                , IntentFactory.createTempChatIntent(context
                        , fromUid, fromNickName
//						, tcmd.getUser_mail()
                )//new Intent()//IntentFactory.createPortalIntent(context)
                , R.drawable.main_alarms_tenpchat_message_icon
                , context.getResources().getString(R.string.notification_temp_text_chatting_message_ticker_text)
                , context.getResources().getString(R.string.notification_temp_text_chatting_message_info_title)
                , MessageFormat.format(context.getResources().getString(R.string.notification_temp_text_chatting_message_info_text)
                        , fromNickName
                        , messageContentForShow)//tcmd.getM())
                , true, true, false);
    }

    /**
     * 收到一个群聊天消息哦.
     *
     * @param context
     */
    public static void showAGroupChatMsgNotivication(Context context
            , boolean isWordChat
            , int msgType
//			, MsgBody4Guest tcmd
            , String msg
            , String fromNickName, String toGid, String toGname
    ) {
        // 此字段将用于消息内容的显示
        String messageContentForShow = MessageExt.parseMessageContentPreview(context, msg, msgType);

        boolean nicknameIsEmpty = CommonUtils.isStringEmpty(fromNickName, true);

        String tickerText = isWordChat ? context.getResources().getString(
                R.string.notification_bbs_text_chatting_message_ticker_text) : "收到1条群聊消息!";
        String infoContentTitle = isWordChat ? context.getResources().getString(
                R.string.notification_bbs_text_chatting_message_info_title) : toGname;
        String infoContentText = isWordChat ? MessageFormat.format(context.getResources().getString(R.string.notification_bbs_text_chatting_message_info_text), fromNickName, messageContentForShow)
                : (nicknameIsEmpty ? "" : fromNickName + " 说:") + messageContentForShow;

        NotificationHelperEx.addNotificaction(
                NOTIFICATION_UNIQE_IDENT_ID_RECIEVED$BBS$MESSAGE, context
                , IntentFactory.createGroupChatIntent(context, toGid, toGname)//new Intent(context, GroupChattingActivity.class)
                , R.drawable.main_alarms_bbschat_message_icon
                , tickerText
                , infoContentTitle
                , infoContentText
                , true, true, false);
    }

    /**
     * 【收到实时语音请求处理方式3】相关处理界面处于后台时接收实时语音聊天请求时的提示（来自发起人A）.
     *
     * @param context
     * @param friendNickName 昵称
     */
    public static void showRealTimeVoiceRequestNotivication(Context context
            , String friendUID, String friendNickName) {
        NotificationHelperEx.addNotificaction(
                NOTIFICATION_UNIQE_IDENT_ID_REAL$TIME$VOICE$CHAT$REQUEST, context
                , IntentFactory.createChatIntent(context, friendUID, true, System.currentTimeMillis())
                , R.drawable.main_alarms_chat_opr_realtime_voice_icon
                , MessageFormat.format(context.getResources().getString(R.string.notification_real_time_chat_request_ticker_text), friendNickName)
                , context.getResources().getString(R.string.notification_real_time_chat_request_info_title)
                , MessageFormat.format(context.getResources().getString(R.string.notification_real_time_chat_request_info_text), friendNickName)
                , true, true, true);
    }


    //---------------------------------------------------------------------------------------------

    /**
     * 尝试清除本除程序产生的所有Notification.这通常需要在APP正常退或者崩溃时调用之.
     * <p>
     * 为何要清除？因为当APP退出后，如果还遗留这些东西，则再点击它时必然会产生崩溃，因为
     * APP都退出了，所有的数据等都不存在了！
     */
    public static void cancalAllNotification(Context context) {
        NotificationManager notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notiManager != null) {
//			notiManager.cancel(NOTIFICATION_UNIQE_IDENT_ID_APP);
            notiManager.cancel(NOTIFICATION_UNIQE_IDENT_ID_ADD$FRIEND$REQUEST);
            notiManager.cancel(NOTIFICATION_UNIQE_IDENT_ID_ADD$FRIEND$REQUEST_RESPONSE$FOR$ERROR_SERVER$TO$A);
            notiManager.cancel(NOTIFICATION_UNIQE_IDENT_ID_NEW$FRIEND$ADD$SUCESS);
            notiManager.cancel(NOTIFICATION_UNIQE_IDENT_ID_ADD$FRIEND$BE$REJECT);
            notiManager.cancel(NOTIFICATION_UNIQE_IDENT_ID_VOICE$VIDEO$CHAT$REQUEST);
            notiManager.cancel(NOTIFICATION_UNIQE_IDENT_ID_RECIEVED$FRIEND$SCENSE$CMD);
            notiManager.cancel(NOTIFICATION_UNIQE_IDENT_ID_RECIEVED$FRIEND$MESSAGE);
            notiManager.cancel(NOTIFICATION_UNIQE_IDENT_ID_REAL$TIME$VOICE$CHAT$REQUEST);
            notiManager.cancel(NOTIFICATION_UNIQE_IDENT_ID_RECIEVED$TEMP$MESSAGE);
            notiManager.cancel(NOTIFICATION_UNIQE_IDENT_ID_RECIEVED$BBS$MESSAGE);
            notiManager.cancel(NOTIFICATION_UNIQE_IDENT_ID_MYSELF$BE$INVITED);

//			notiManager.cancel(R.string.ident_genius_services_started);//
        }
    }
}
